package com.interest.modules.packagebranch.entity;

import com.interest.base.common.BuilderUtil;

/**
 * Created by yangfeng on 16-11-15.
 */
public class PackageVersion {
    private static final char JOINT_CHAR = '-';
    private static final String SUFFIX = "p";

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    private String versionName;
    private int versionCode;

    public String toString() {
        return versionName + JOINT_CHAR + versionCode + SUFFIX;
    }

    public static PackageVersion from(String text) {
        String[] versionSegments = BuilderUtil.getMinusSegments(text);
        if (null == versionSegments || versionSegments.length != 2) {
            return null;
        }

        PackageVersion version = new PackageVersion();
        version.setVersionName(versionSegments[0]);
        version.setVersionCode(Integer.parseInt(versionSegments[1].toLowerCase().replace("p", "")));

        return version;
    }
}